VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form frmWorkingSheet 
   Caption         =   "Working Sheet"
   ClientHeight    =   7020
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   14535
   LinkTopic       =   "Form1"
   MDIChild        =   -1  'True
   ScaleHeight     =   7020
   ScaleWidth      =   14535
   WindowState     =   2  'Maximized
   Begin VB.Frame Frame2 
      Caption         =   "HISTORY"
      Height          =   9615
      Left            =   10080
      TabIndex        =   1
      Top             =   360
      Width           =   9255
      Begin VB.CommandButton cmdAll 
         Caption         =   "ALL Sheets"
         Height          =   495
         Left            =   7320
         TabIndex        =   19
         Top             =   8520
         Width           =   1335
      End
      Begin MSFlexGridLib.MSFlexGrid Grid 
         Height          =   7815
         Left            =   240
         TabIndex        =   13
         Top             =   360
         Width           =   8775
         _ExtentX        =   15478
         _ExtentY        =   13785
         _Version        =   393216
         Rows            =   100
         Cols            =   7
         FixedCols       =   0
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "DETAILS OF THE JOB YOU HAVE DONE!!!"
      Height          =   9615
      Left            =   120
      TabIndex        =   0
      Top             =   360
      Width           =   9615
      Begin VB.CommandButton cmdNew 
         Caption         =   "New"
         Height          =   495
         Left            =   3840
         TabIndex        =   18
         Top             =   8760
         Visible         =   0   'False
         Width           =   1335
      End
      Begin VB.CommandButton cmdDel 
         Caption         =   "Delete"
         Height          =   495
         Left            =   5640
         TabIndex        =   17
         Top             =   8760
         Visible         =   0   'False
         Width           =   1335
      End
      Begin VB.TextBox txtHrs 
         Appearance      =   0  'Flat
         Height          =   375
         Left            =   1800
         TabIndex        =   14
         Top             =   2760
         Width           =   2535
      End
      Begin VB.CommandButton cmdAdd 
         Caption         =   "Add modules"
         Height          =   495
         Left            =   6360
         TabIndex        =   12
         Top             =   1440
         Visible         =   0   'False
         Width           =   1335
      End
      Begin VB.CommandButton cmdSubmit 
         Caption         =   "Submit"
         Height          =   495
         Left            =   7440
         TabIndex        =   11
         Top             =   8760
         Width           =   1335
      End
      Begin VB.TextBox txtDes 
         Appearance      =   0  'Flat
         Height          =   2655
         Left            =   1800
         MultiLine       =   -1  'True
         ScrollBars      =   2  'Vertical
         TabIndex        =   10
         Top             =   5400
         Width           =   7335
      End
      Begin VB.TextBox txtPg 
         Appearance      =   0  'Flat
         Height          =   375
         Left            =   1800
         TabIndex        =   7
         Text            =   "100"
         Top             =   4200
         Width           =   2535
      End
      Begin VB.ComboBox cmbModule 
         Height          =   315
         Left            =   1800
         TabIndex        =   5
         Text            =   "Combo1"
         Top             =   1560
         Width           =   2535
      End
      Begin VB.TextBox txtName 
         Appearance      =   0  'Flat
         Enabled         =   0   'False
         Height          =   375
         Left            =   1800
         TabIndex        =   3
         Top             =   600
         Width           =   2535
      End
      Begin VB.Label Label7 
         Caption         =   "Time:"
         Height          =   375
         Left            =   600
         TabIndex        =   16
         Top             =   2760
         Width           =   1815
      End
      Begin VB.Label Label6 
         Caption         =   "How many hours do you need?"
         ForeColor       =   &H000000FF&
         Height          =   375
         Left            =   4560
         TabIndex        =   15
         Top             =   2760
         Width           =   1815
      End
      Begin VB.Label Label5 
         Caption         =   "Description:"
         Height          =   375
         Left            =   600
         TabIndex        =   9
         Top             =   5280
         Width           =   1815
      End
      Begin VB.Label Label4 
         Caption         =   "How much you have done?"
         ForeColor       =   &H000000FF&
         Height          =   375
         Left            =   4560
         TabIndex        =   8
         Top             =   4200
         Width           =   1815
      End
      Begin VB.Label Label3 
         Caption         =   "Percentage:"
         Height          =   375
         Left            =   600
         TabIndex        =   6
         Top             =   4200
         Width           =   1815
      End
      Begin VB.Label Label2 
         Caption         =   "Module:"
         Height          =   375
         Left            =   600
         TabIndex        =   4
         Top             =   1560
         Width           =   1815
      End
      Begin VB.Label Label1 
         Caption         =   "Name:"
         Height          =   375
         Left            =   600
         TabIndex        =   2
         Top             =   720
         Width           =   1815
      End
   End
End
Attribute VB_Name = "frmWorkingSheet"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim iItem As Integer
Dim bUpdate As Boolean

Dim bAllSheet As Boolean

Private Sub cmdAdd_Click()
    Dim sql, inputCnt As String
    inputCnt = InputBox("Please input a new module:", SOFT_TITLE)
    If inputCnt <> "" Then
        sql = "INSERT INTO `Module`(`ModuleName`) VALUES ('" & inputCnt & "')"
        Call CloseRecordSet(rs)
        OperateDatabase rs, cn, sql
        
        MsgBox "New module added!", vbOKOnly, SOFT_TITLE
        Load_Modules
    End If
End Sub

Private Sub cmdAll_Click()
    bAllSheet = Not bAllSheet
    If bAllSheet Then
        cmdAll.Caption = "Single Sheet"
    Else
        cmdAll.Caption = "All Sheets"
    End If
    frmSplash.Show
    cmdAll.Enabled = False
    DoEvents
    init_grid
    load_history
    DoEvents
    Unload frmSplash
    cmdAll.Enabled = True
End Sub

Private Sub cmdDel_Click()
    Dim sql As String
    cmdDel.Visible = False
    frmSplash.Show
    DoEvents
    
    sql = "UPDATE `WorkingSheet` SET `bDel`=1 WHERE ID=" & iItem
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
    DoEvents
    
    DoEvents
    MsgBox "Deleted!", vbOKOnly, SOFT_TITLE
    Unload frmSplash
    init_grid
    load_history
    cmdNew_Click
End Sub

Private Sub cmdNew_Click()
    cmdNew.Visible = False
    cmbModule.ListIndex = 0
    txtHrs.Text = ""
    txtDes.Text = ""
    txtPg.Text = ""
    cmdSubmit.Caption = "Submit"
    cmdDel.Visible = False
    bUpdate = False
End Sub

Private Sub cmdSubmit_Click()
    Dim iHrs As Integer
    Dim iPg As Integer
    Dim sql As String
    Dim DateString As String
    
    
    ihr = txtHrs.Text
    'If iHrs <= 0 Or iHrs > 1000 Then
     '   MsgBox "Hour entered was wrong, please check!", vbOKOnly, SOFT_TITLE
     '   Exit Sub
    'End If
    
    iPg = txtPg.Text
    If iPg < 1 Or iPg > 100 Then
        MsgBox "Percentage entered was wrong, the number should between 1 and 100!", vbOKOnly, SOFT_TITLE
        Exit Sub
    End If
    
    If txtDes.Text = "" Then
        MsgBox "The description is empty! Please write something!", vbOKOnly, SOFT_TITLE
        Exit Sub
    End If
    
    If bUpdate Then
        sql = "UPDATE `WorkingSheet` SET `usr_id`=" & Operator_ID & ",`content`='" & AddSlashes(txtDes.Text) & "',`module_id`=" & cmbModule.ListIndex & ",`percentage`=" & Val(txtPg.Text) & ",`TimeCost`= " & Val(txtHrs.Text) & " WHERE ID = " & iItem & " limit 1"
        Call CloseRecordSet(rs)
        OperateDatabase rs, cn, sql
        cmdNew_Click
        cmdDel.Visible = False
    Else
        DateString = Year(Date) & Month(Date) & Day(Date)
        sql = "INSERT INTO `WorkingSheet`(`usr_id`, `upload_date`, `content`, `module_id`, `percentage`, `TimeCost`) VALUES (" & Operator_ID & ", '" & DateString & "',  '" & AddSlashes( _
        txtDes.Text) & "', " & cmbModule.ListIndex & ", " & Val(txtPg.Text) & "," & Val(txtHrs.Text) & ")"
        Call CloseRecordSet(rs)
        OperateDatabase rs, cn, sql
    End If
    Grid.Clear
    init_grid
    load_history
    
    MsgBox "Operation Done!", vbOKOnly, SOFT_TITLE
End Sub

Private Sub Form_Load()
    If Operator_ACC <> 5 Then
        cmdAll.Enabled = False
    End If
    
    bAllSheet = False
    bUpdate = False
    iItem = -1
    frmSplash.Show
    DoEvents
    On Error GoTo openDB
    sql = "SET NAMES gbk"
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
openDB:
    frmWorkingSheet.Caption = "Working Sheet: " & Operator
    Call Cn2SQL(cn)
    Load_Modules
    
    If Operator_ACC = 3 Or Operator_ACC = 5 Then
        'cmdAdd.Visible = True
    End If
    
    txtName.Text = Operator
    
    init_grid
    load_history
    DoEvents
    Unload frmSplash
End Sub

Private Sub load_history()
    Dim sql As String
    Dim iX, iY As Integer
    Grid.Clear
    If Not bAllSheet Then
    sql = "select users.username, WorkingSheet.upload_date, Module.ModuleName, WorkingSheet.TimeCost, WorkingSheet.percentage, WorkingSheet.content, WorkingSheet.ID  from users, WorkingSheet, Module where users.ID=WorkingSheet.usr_id and WorkingSheet.module_id = Module.ID and WorkingSheet.bDel=0 and WorkingSheet.usr_id =" & Operator_ID
    Else
    sql = "select users.username, WorkingSheet.upload_date, Module.ModuleName, WorkingSheet.TimeCost, WorkingSheet.percentage, WorkingSheet.content, WorkingSheet.ID  from users, WorkingSheet, Module where users.ID=WorkingSheet.usr_id and WorkingSheet.module_id = Module.ID and WorkingSheet.bDel=0 "
    End If
     Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
     
    iX = 0
    iY = 1
   ' rs.MoveFirst
    While Not rs.EOF
        DoEvents
        iX = 0
        With Grid
            .TextMatrix(iY, iX) = rs.Fields(0).value
            iX = iX + 1
            .TextMatrix(iY, iX) = rs.Fields(1).value
            iX = iX + 1
            .TextMatrix(iY, iX) = rs.Fields(2).value
            iX = iX + 1
            .TextMatrix(iY, iX) = str(rs.Fields(3).value) & " Hrs"
            iX = iX + 1
            .TextMatrix(iY, iX) = str(rs.Fields(4).value) & " %"
            iX = iX + 1
            .TextMatrix(iY, iX) = rs.Fields(5).value
            iX = iX + 1
            .TextMatrix(iY, iX) = rs.Fields(6).value
        End With
        iY = iY + 1
        rs.MoveNext
    Wend
End Sub

Private Sub init_grid()
    With Grid
        .TextMatrix(0, 0) = "Name"
        .ColWidth(0) = .Width * 0.1
        .TextMatrix(0, 1) = "Upload date"
        .ColWidth(1) = .Width * 0.15
        .TextMatrix(0, 2) = "Module"
        .ColWidth(2) = .Width * 0.2
        .TextMatrix(0, 3) = "Time cost"
        .ColWidth(3) = .Width * 0.1
        .TextMatrix(0, 4) = "Percentage"
        .ColWidth(4) = .Width * 0.1
        .TextMatrix(0, 5) = "Description"
        .ColWidth(5) = .Width * 0.5
        .TextMatrix(0, 6) = "Item Number"
        .ColWidth(6) = .Width * 0.2
    End With
End Sub

Private Sub CloseRecordSet(ByRef rs As ADODB.Recordset)
On Error GoTo NoErr
        rs.Close
NoErr:
End Sub
Private Sub Load_Modules()
    Dim sql As String
    sql = "select * from Module"
    Call CloseRecordSet(rs)
    OperateDatabase rs, cn, sql
    cmbModule.Clear
    While Not rs.EOF
        cmbModule.AddItem rs.Fields(1).value
        rs.MoveNext
    Wend
    
    cmbModule.ListIndex = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
    cn.Close
End Sub

Private Sub Text1_Change()

End Sub

Private Sub Grid_Click()
    With Grid
        .SelectionMode = flexSelectionByRow
    End With
End Sub

Private Sub Grid_DblClick()
    If Not bAllSheet Then
        If Grid.TextMatrix(Grid.Row, 0) = "" Then
            MsgBox "Please select a valid record!", vbOKOnly, SOFT_TITLE
            Exit Sub
        End If
        cmdDel.Visible = True
        cmdNew.Visible = True
        bUpdate = True
        cmdSubmit.Caption = "Update"
        
        With Grid
        txtName.Text = .TextMatrix(.Row, 0)
        txtHrs.Text = Mid(.TextMatrix(.Row, 3), 1, Len(.TextMatrix(.Row, 3)) - 3)
        cmbModule.ListIndex = GetCmbIndex(.TextMatrix(.Row, 2))
        txtPg.Text = Mid(.TextMatrix(.Row, 4), 1, Len(.TextMatrix(.Row, 4)) - 1)
        txtDes.Text = .TextMatrix(.Row, 5)
        End With
        
        GetRecordID iItem
    Else
        MsgBox "Please switch to single sheet mode!", vbOKOnly, SOFT_TITLE
    End If
End Sub

Private Function GetCmbIndex(ByVal s As String) As Integer
    Dim i As Integer
    i = 0
    While cmbModule.List(i) <> ""
        If cmbModule.List(i) = s Then
            GetCmbIndex = i
            Exit Function
        End If
        i = i + 1
    Wend
    
    GetCmbIndex = 0
End Function

Private Sub GetRecordID(ByRef iItem As Integer)
    iItem = Val(Grid.TextMatrix(Grid.Row, 6))
End Sub
